//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.kun.portal.webframe.controller;

import com.kun.framework.core.util.DateUtils;
import com.kun.framework.web.web.vo.ValueLabelVo;
import com.kun.portal.webframe.po.FwRolePo;
import com.kun.portal.webframe.po.FwTeamPo;
import com.kun.portal.webframe.po.FwUserPo;
import com.kun.portal.webframe.service.FwRoleService;
import com.kun.portal.webframe.util.FwBaseUtil;
import com.kun.portal.webframe.util.HeaderUtil;
import com.kun.portal.webframe.util.UserSessionUtil;
import com.kun.portal.webframe.vo.RoleTeamVo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.ResponseEntity.BodyBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({"/api/webframe"})
public class FwRoleController {
    private static final Logger LOG = LoggerFactory.getLogger(FwRoleController.class);
    @Autowired
    private FwRoleService fwroleService;

    public FwRoleController() {
    }

    @RequestMapping(
            value = {"/fwrole"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public Page<FwRolePo> queryByCreatorAndRoleTypeAndRoleNameContainingOrderByRoleCode(@RequestParam String type, @RequestParam(required = false) String key, Pageable pageable, HttpSession session) {
        key = key == null?"":key;
      //  FwTeamPo fwTeam = UserSessionUtil.getCurrentUserTeam(session);
        return this.fwroleService.queryWithParams(null, key, pageable);
    }

    @RequestMapping(
            value = {"/fwrole/check"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public List<FwRolePo> check(@RequestBody Map<String, String> map, HttpSession session) {
        return this.fwroleService.check(map);
    }

    @RequestMapping(
            value = {"/fwrole/transferByUser/{userId}"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public Map queryRoleByUserId(@PathVariable("userId") String userId, HttpSession session) {
        Map result = new HashMap();
        //FwTeamPo fwTeam = UserSessionUtil.getCurrentUserTeam(session);
        List<RoleTeamVo> allcreated = this.fwroleService.findFwRoleByTeam((FwTeamPo) null);
        List<String> selected = this.fwroleService.findFwRoleByUserId(userId);
        result.put("dataRoles", allcreated);
        result.put("myRoles", selected);
        return result;
    }

    @RequestMapping(
            value = {"/fwrole/transferByModule/{moduleId}"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public Map queryRoleByModuleId(@PathVariable("moduleId") String moduleId, HttpSession session) {
        Map result = new HashMap();
        FwUserPo user = UserSessionUtil.getCurrentUser(session);
        List<FwRolePo> allcreated = this.fwroleService.findFwRoleByCreator(user.getUserId());
        List<String> selected = this.fwroleService.findFwRoleByModuleId(moduleId);
        result.put("dataRoles", allcreated);
        result.put("myRoles", selected);
        return result;
    }

    @RequestMapping(
            value = {"/fwrole/transferByTeam/{teamId}"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public Map queryRoleByTeamId(@PathVariable("teamId") String teamId, HttpSession session) {
        Map result = new HashMap();
        FwUserPo user = UserSessionUtil.getCurrentUser(session);
        List<FwRolePo> allcreated = this.fwroleService.findFwRoleByCreator(user.getUserId());
        //List<String> selected = this.fwroleService.findFwRoleByTeamId(teamId);
        result.put("dataRoles", allcreated);
        //result.put("myRoles", selected);
        return result;
    }

    @RequestMapping(
            value = {"/fwrole/queryRoleByTeamIdAndUserId"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public List queryRoleByTeamIdAndUserId(HttpSession session) {
        FwUserPo user = UserSessionUtil.getCurrentUser(session);
        FwTeamPo fwTeam = UserSessionUtil.getCurrentUserTeam(session);
        return this.fwroleService.findFwRoleByUserIdAndTeamId(user.getUserId(), fwTeam.getTeamId());
    }

    @RequestMapping(
            value = {"/fwrole/updateFwRoleUserByUserId"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public Map updateFwRoleUserByUserId(@RequestBody Map map, HttpSession session) {
        List<String> roleIds = (List)map.get("roleIds");
        String userId = (String)map.get("userId");
        this.fwroleService.updateFwRoleUserByUserId(roleIds, userId);
        return null;
    }

    @RequestMapping(
            value = {"/fwrole/updateFwRoleUserByRoleId"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public void updateFwRoleUserByRoleId(@RequestBody Map map, HttpSession session) {
        String roleId = (String)map.get("roleId");
        List<Map<String, String>> list = (List)map.get("userList");
        List<FwUserPo> userList = new ArrayList();
        Iterator var7 = list.iterator();

        while(var7.hasNext()) {
            Map<String, String> user = (Map)var7.next();
            FwUserPo userPo = new FwUserPo();
            userPo.setUserId((String)user.get("userId"));
            userList.add(userPo);
        }

        this.fwroleService.updateFwRoleUserByRoleId(roleId, userList);
    }

    @RequestMapping(
            value = {"/fwrole"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public ResponseEntity<FwRolePo> addFwRole(@RequestBody FwRolePo fwrole, HttpSession session) {
        fwrole.setCreator(FwBaseUtil.getUserId(session));
        fwrole.setCreateTime(DateUtils.getNowDate());
        fwrole.setModifyTime(DateUtils.getNowDate());
        FwRolePo metaFwRole = this.fwroleService.addFwRole(fwrole);
        return new ResponseEntity(metaFwRole, HttpStatus.OK);
    }

    @RequestMapping(
            value = {"/fwrole"},
            method = {RequestMethod.PUT}
    )
    @ResponseBody
    public ResponseEntity<FwRolePo> updateFwRole(@RequestBody FwRolePo fwrole, HttpSession session) {
//        if(StringUtils.isEmpty(fwrole.getRoleId())) {
//            return this.addFwRole(fwrole, session);
//        } else {
////            fwrole.setModifyTime(DateUtils.getNowDate());
////            FwRolePo metaFwRole;
////            if(fwrole.getUserList() == null) {
////                metaFwRole = this.fwroleService.findOne(fwrole.getRoleId());
////                fwrole.setUserList(metaFwRole.getUserList());
////            }
//
//            metaFwRole = this.fwroleService.updateFwRole(fwrole);
//            return new ResponseEntity(metaFwRole, HttpStatus.OK);
//        }
        return null;
    }

    @RequestMapping(
            value = {"/fwrole/{roleId}"},
            method = {RequestMethod.DELETE}
    )
    @ResponseBody
    public ResponseEntity<Void> deleteFwRole(@PathVariable("roleId") String roleId) {
        this.fwroleService.deleteFwRole(roleId);
        return ((BodyBuilder)ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("fwrole", roleId))).build();
    }

    @RequestMapping(
            value = {"/fwrole/count/{roleId}"},
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public Map countFwRole(@PathVariable("roleId") String roleId) {
        return this.fwroleService.countFwRole(roleId);
    }

    @RequestMapping(
            value = {"/fwrole/updateFwRoleTeamByRoleId"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public Map updateFwRoleTeamByRoleId(@RequestBody Map map, HttpSession session) {
        List<String> teamIds = (List)map.get("teamIds");
        String roleId = (String)map.get("roleId");
        this.fwroleService.updateFwRoleTeamByRoleId(teamIds, roleId);
        return null;
    }
}
